package apka.SMSBillet.CTR;

import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import apka.SMSBillet.DB.DataBaseHelper;

public class CTRdatabase {
	private Context context;
	private boolean isCitiesLoaded = false;
	private CTRcity ctrCity;
	private static HashMap<Integer, String> cityIdMap;
	
	public CTRdatabase(Context context){
		this.context = context;
	}
	
    public Cursor getCities() {
    	DataBaseHelper myDbHelper = new DataBaseHelper(context);
    	myDbHelper.openDataBase();
    	Cursor c = myDbHelper.getAllCities();
    	c.moveToFirst();
    	myDbHelper.close();
    	return c;
	}    

    public HashMap<Integer, String> getCityIdMap(){
    	Cursor c = getCities();
    	cityIdMap = new HashMap<Integer, String>();
    	for (int i = 0; i < c.getCount(); i++) {	
    		cityIdMap.put(c.getInt(c.getColumnIndex("ZoneNumber")), c.getString(c.getColumnIndex("City")));
    		c.moveToNext();
		}
    	return cityIdMap;
    }
    
    public int getCityId(String city){
    	int result = 0;
    	DataBaseHelper myDbHelper = new DataBaseHelper(context);
    	myDbHelper.openDataBase();
    	city = city.trim();
    	Cursor c = myDbHelper.getCityID(city);
    	if(c.moveToFirst())
    	{
    		result = c.getInt(c.getColumnIndex("zoneId"));
    	}
    	myDbHelper.close();
    	return result;
    }
    
    public int getZonesBetween(String cityFrom, String cityTo){
    	int result = 0;
    	DataBaseHelper myDbHelper = new DataBaseHelper(context);
    	myDbHelper.openDataBase();
    	
    	int cityFromNumber = getCityId(cityFrom);
    	
    	Cursor c = myDbHelper.getZonesBetween(cityFromNumber, getCityId(cityTo));
    	if(c.moveToFirst())
    	{
    		result = c.getInt(c.getColumnIndex("z"+cityFromNumber));
    	}
    	myDbHelper.close();
    	return result; 
    }

}
